const fs = require('fs');
const path = require('path');

function deleteRecursive(target) {
    if (fs.existsSync(target)) {
        if (fs.lstatSync(target).isDirectory()) {
            // 是目录，递归删除
            fs.readdirSync(target).forEach(file => {
                const curPath = path.join(target, file);
                deleteRecursive(curPath);
            });
            fs.rmdirSync(target);
            console.log("成功删除目录: " + target);
        } else {
            // 是文件，直接删除
            fs.unlinkSync(target);
            console.log("成功删除文件: " + target);
        }
    } else { 
        console.log("路径不存在: " + target);
        process.exit(1);
    }
}

// 获取命令行参数
const targetPath = process.argv[2];

if (!targetPath) {
    console.log('请指定要删除的文件或目录路径');
    console.log('用法: node delManager.js <文件或目录路径>');
    process.exit(1);
}

// 解析为绝对路径
const absolutePath = path.resolve(targetPath);

console.log("开始删除: " + absolutePath);
deleteRecursive(absolutePath);
console.log('删除操作完成');